<?php
/*
*分类模块
*/
class Category_model extends CI_Model{
	private $level =1;//层级数
	public function __construct(){
		$this->load->database();
	}
	
	
	/*
	*	创建分类
	*/
	public function add(){
	
		if($this->db->insert('category', $this->input->post())) {
			return true;
		}else{
			return false;
		}
			
	}
	
	/*
	*	修改分类
	*/
	public function edit($category_id,$date){
		
		$this->db->where('category_id',$category_id);
		
		if($this->db->update('category',$date)) {
			return true;
		}else{
			return false;
		}
	
	}
	
	/*
	*删除分类
	*/
	public function delete($category_id){
	
		$ids = explode(',',$category_id);
		
		//1.转移分类，当前分类下的子分类需要转移到特定分类
	// foreach($ids as $id){
		// $this->getChildDir($id);
	// }
	
		//2.转移内容，如当前分类下的文章等需要转移到指定分类
		
		//3.删除分类
		$this->db->where_in('category_id',$ids);
		$this->db->delete('category');
		
		$rows = $this->db->affected_rows();
		if($rows >= 1){
			return true;
		}else{
			return false;
		}
		/*
		* 其他操作,如当前目录下文档自动向其他类别归类，
		*/
	
	}
	
	
	
	/*
	*查询单个目录信息
	*/
	public function getOneCate($cate_id){
		$this->db->where('category_id',$cate_id);
		$cate = $this->db->get('category');
		if($cate){
			return $cate->row_array();
		}
	}
	

	
	public function getFather($cate_id){
		$sql="select * from dir where dir_id=$cate_id";
		if($dir = $this->db->query($sql)){
		
		}
	}
	
		
	
	/*
	*判断是否有子节点
	*
	*/
	public function isFatherDir($parent_id){
		$this->db->where('parent_id',$parent_id);
		if($cates = $this->db->get('category') ){
			if(count($cates->result_array()) > 0){
				return true;	
			}else{
				return false;
			}
			
		}
	}
	
	/*
	*查询目录下的子目录
	*/
	public function getChildDir($parent_id){
	
		$result =array();
		global $i;
		$this->db->where('parent_id',$parent_id);
		if($r = $this->db->get('category')){
			foreach($r->result_array() as $dir){//遍历分类下所有子分类
				$i++;
				$result[$dir['category_id']] = $dir;
				if($this->isFatherDir($dir['category_id'])){
					$result[$dir['category_id']]['child']= $this->getChildDir($dir['category_id']);
				}
				
			}
			
			return $result;
		}
			
	}

	
	
	public function getAllCategory2(){
		$cates = array();
		$this->db->order_by('ordernum','desc');
		if( $dir = $this->db->get('category') ){
			foreach($dir->result_array() as $cate){
			
				//创建数组第一维，存入所有节点的全部信息
				$cates[$cate['category_id']] = $cate;
		
			}
			
			$temp = array();
	
			//通过遍历数组
			foreach($cates as $category_id => $cate){
			
				//当遇到子节点
				if($cate['parent_id'] >0){
		
					//把子节点存到相应的父节点
					$cates[$cate['parent_id']]['child'][$cate['category_id']] = &$cates[$cate['category_id']];
					$temp[] = $category_id;
				
				}
			}
			
			
			//剔出一维数组中不是顶级分类的内容
			foreach($temp as $t){
				unset($cates[$t]);
			}
		}

		return $cates;
	}
	
	//获取全部分类，仅用于分类列表
	public function getTree($cates,$level = 1){
		
		 foreach($cates as $cate){
		 

           ?>
		   
             <tr class="list-article">
				<td><input type="checkbox" name="checkbox_category" value="<?php echo $cate['category_id'];?>" /></td>
				<td><?php echo $cate['category_id'];?></td>
				<td><span style='display:inline-block;width:<?php echo  $level *25;?>px;'>&nbsp;&nbsp;</span><?php echo "|--".$cate['category_name'];?><span class="badge"><?php echo  $cate['posts_count'];?></span></td>
				<td><?php echo $cate['ordernum'];?></td>
					<td>
				<?php
					switch($cate['status']){
						case 0:
				?>
						<a href="javascript:status(<?php echo $cate['category_id']?>,1)"><span class="label label-important" title="点击启用">已禁用</span></a>
				<?php
						
						break;
						case 1:
				?>
						<a href="javascript:status(<?php echo $cate['category_id']?>,0)"><span class="label label-success"  title="点击禁用">已启用</span></a>
				<?php
						break;
					}
				?>
				</td>
				<td><a href="/<?php echo $this->config->item('index_page');?>/category/edit/<?php echo $cate['category_id']?>" title='编辑'><i class="icon icon-edit"></i></a>&nbsp;&nbsp;<a href="#" title='禁用'><i class="icon icon-ban-circle"></i></a>&nbsp;&nbsp;<a href="javascript:del(<?php echo $cate['category_id']?>)" title='删除'><i class="icon icon-remove"></a></td>
			</tr>
				
           <?php
				if(isset($cate['child'])){
					$this->getTree($cate['child'],$level+1);
				}
			
         }
	
			
	}

	
	/*
	* 输出select下拉列表
	* 参数:
	* $cates:待选的全部分类
	*  $category_id: @array or @int 当前分类的id，用于编辑分类,可以为数值或数组
	* $parent_id:当前分类的父级分类id，用于编辑分类
	* $level string 分类的相对深度，用以有层次的输出前端节点
	*/
	public function getTree2($cates,$category_id =0,$parent_id=0,$level=0){
		
		$category_id_arr =array();
		
		/*
		* 文章所属分类
		* 编辑文章时，传入category_id,用以设置相应的分类为选中		
		*/
		if(is_array($category_id)){
			$category_id_arr = $category_id;
		}else{
		
		/*
		* 分类的父分类
		* 编辑分类时，传入分类的父分类，以便设置相应的分类为选中状态
		*/
			array_push($category_id_arr,$parent_id);
			
		}
		
		foreach($cates as $cate){
			
			//过滤出待选分类设置为选中
			if(in_array($cate['category_id'],$category_id_arr)){
			
				//设置为选中状态
					echo "<option value='".$cate['category_id']."' selected='selected'>";
			
			}else{
				
				echo "<option value='".$cate['category_id']."'>";		
			}

			for($i=0;$i<$level;$i++){
				echo "&nbsp;&nbsp;&nbsp;&nbsp;";
			}
			echo "|--".$cate['category_name'];

			if($cate['parent_id'] == 0){
				echo "(顶级分类)";
			}
			
			echo "</option>";
			
			if(isset($cate['child'])){
		
				//递归处置
				$this->getTree2($cate['child'],$category_id,$parent_id,$level+1);
			}
			
			
		}
		
	}
	
	
	/*
	* 输出checkbox 树结构
	* 参数:
	* $cates:待选的全部分类
	*  $category_id: @array or @int 当前分类的id，用于编辑分类,可以为数值或数组
	* $parent_id:当前分类的父级分类id，用于编辑分类
	* $level string 分类的相对深度，用以有层次的输出前端节点
	*/
	public function  getTreeCheckbox($cates,$name,$category_id =0,$parent_id=0,$level=0){
		$category_id_arr =array();
		
		//文章所属分类
		if(is_array($category_id)){
			$category_id_arr = $category_id;
		}else{
		//分类的父分类
			array_push($category_id_arr,$parent_id);
		}
		
		foreach($cates as $cate){
				for($i=0;$i<$level;$i++){
				echo "&nbsp;&nbsp;&nbsp;&nbsp;";
			}
			//所属分类包含当前分类
			if(in_array($cate['category_id'],$category_id_arr)){
				//输出为选中状态
					echo "<input type='checkbox' name='$name' value='".$cate['category_id']."' checked='checked'/>";
			}else{
				
				echo "<input type='checkbox' name='$name'  value='".$cate['category_id']."'>";

			}

		
			echo $cate['category_name'];
			if($cate['parent_id'] == 0){
				echo "(顶级分类)";
			}
			
			echo "<br/>";
		
			if(isset($cate['child'])){
		
				$this->getTreeCheckbox($cate['child'],$name,$category_id,$parent_id,$level+1);
			}
			
		}
		
	}
	
		
	//更新状态
	public function up_status($id,$status){
		
		$ids = explode(',',$id['id']);
		$this->db->where_in('category_id',$ids);
		$this->db->update('category',$status);

		if($this->db->affected_rows() > 0){
			return true;
		}else{
			return false;
		}
	}
			
}